import React from 'react';
import classNames from 'classnames';

import DialogCommon from './DialogCommon';
import { default as DialogPhone } from './dialog/DialogPhone';

import * as Shell from '../common/Shell';

class Telephone extends React.Component {

  constructor() {
    super();

    this.state = {
       phoneVisiable : true,

       dialogVisiable : false,
       dialogTitle : "系统提示",
       dialogContent : "",
       dialogButton : null
    };
  }

  static propTypes = {
      text : React.PropTypes.string,
      isClose : React.PropTypes.bool,
      user : React.PropTypes.object
  }

  static defaultProps = {
      text : "更多股票咨询，请致电您的专属理财顾问",
      isClose : true,
      user : null
  }

  handlePhoneClick(event){

    let { user } = this.props;
    let phoneData = null;
    if (user.jjrdh && user.jjrxm) {
      phoneData = {
        name : user.jjrxm,
        phone : user.jjrdh
      };
    }else{
      let randomPhone = (Math.random()<0.5)?"028-85253045":"028-85259495";
      phoneData = {
        name : "华西证券",
        phone : randomPhone
      }
    }

    this.setState({
      phoneVisiable : false,
      dialogVisiable : true,
      dialogTitle : `您的专属理财顾问`,
      dialogContent : <DialogPhone phoneData = {phoneData}/>,
      dialogButton: [{
         text : "马上通话",
         style : "info",
         callback : function(){
            //调用手机通话功能
            Shell.callShellMethod("callPhonePlugin", null, null,  { phone : phoneData.phone } );
            this.handleDialogHide();
         }.bind(this)
      },{
         text : "取消",
         style : "default"
      }]
    });
    event.stopPropagation();
  }

  handleDialogHide(){
    this.setState({
       dialogVisiable : false
    });
  }

  handleCloseClick(event){
    event.stopPropagation();
    
    this.setState({
      phoneVisiable : false,
    });
  }

  render() {
    let { text, isClose } = this.props;

    let phoneClass = classNames("manager-telphone", { "hide" : !this.state.phoneVisiable });

    return(
        <div className={phoneClass} onClick={this.handlePhoneClick.bind(this)}>
             <i className="icon"></i>
             <span className="text">{ text }</span>
             { 
                isClose ? 
                <a className="close" onClick={this.handleCloseClick.bind(this)}>
                  <i className="iconfont icon-close"></i>
                </a>   
                : null
             }


             <DialogCommon 
                title={ this.state.dialogTitle } 
                content={ this.state.dialogContent } 
                buttons = { this.state.dialogButton }
                visiable={ this.state.dialogVisiable } 
                onHide = { this.handleDialogHide.bind(this) }/>
        </div>
    );
  }
}

export default Telephone;
